<?php


function t($table){
	return $GLOBALS['dbpre'].$table;
}

//json格式化
$err_msg = array();
function to_json($state='1',$err_code='000',$data){
    header('Content-Type:application/json; charset=utf-8');
    $msg = isset($GLOBALS['err_msg'][$err_code]) ? $GLOBALS['err_msg'][$err_code] : $err_code;
	$result = array('state'=>(string)$state, 'err_code'=>(string)$err_code, 'msg'=>(string)$msg, 'data'=>(object)$data);
    //jsonp
    //$callback = $_GET['callback'];
    //exit($callback.'('.json_encode($result).')');
    //json
	exit(json_encode($result));
}

//输入过滤快捷方法
function i($key,$default_val='',$request='get'){
	if($request == 'get'){
		$_GET[$key] = !isset($_GET[$key]) ? $default_val : $_GET[$key];
		return $_GET[$key];
	}
	if($request == 'post'){
		$_POST[$key] = !isset($_POST[$key]) ? $default_val : $_POST[$key];
		return $_POST[$key];
	}
    if($request == 'request'){
        $_REQUEST[$key] = !isset($_REQUEST[$key]) ? $default_val : $_REQUEST[$key];
        return $_REQUEST[$key];
    }
	if($request == 'cookie'){
		$_COOKIE[$key] = !isset($_COOKIE[$key]) ? $default_val : $_COOKIE[$key];
		return $_COOKIE[$key];
	}
	if($request == 'session'){
		$_SESSION[$key] = !isset($_SESSION[$key]) ? $default_val : $_SESSION[$key];
		return $_SESSION[$key];
	}
    return '';
}

//生成唯一订单号
function order_sn()
{
    /* 选择一个随机的方案 */
    mt_srand((double) microtime() * 1000000);
    //return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    return date('Ymd') . uniqid();
}